<?php

/**
 *   OPENSHOP 商品管理程序
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: goods_dopot_seat.php,v 1.0 2009/06/12 07:35:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/admin/includes/lib_goods.php');
$exc = new exchange($os->table('goods'), $db, 'goods_id', 'goods_name');

if ($_REQUEST['act'] == 'list')
{
	//检查权限
	admin_priv();
	
    $smarty->assign('ur_here',      '商品仓库与库位列表');
	$smarty->assign('full_page',    1);
	
	$smarty->assign('cat_list',     cat_list(0, $cat_id));
	$smarty->assign('brand_list',   get_brand_list());
	$smarty->assign('depot_list',   get_depots()); 
	   		    
	$goods_list = goods_list(0, 1);	
	$smarty->assign('goods_list',   $goods_list['goods']);
	$smarty->assign('filter',       $goods_list['filter']);
	$smarty->assign('record_count', $goods_list['record_count']);
	$smarty->assign('page_count',   $goods_list['page_count']);

	/* 显示商品列表页面 */
	assign_query_info();

	$smarty->display('goods_dopot_seat.tpl');	
}

elseif ($_REQUEST['act'] == 'query')
{
	$smarty->assign('depot_list',   get_depots()); 
	
    $list = goods_list(0, 1);    
    $smarty->assign('goods_list',     $list['goods']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);
    
    make_json_result($smarty->fetch('goods_dopot_seat.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif($_REQUEST['act']=='filter_seat_range'){
    if(!is_numeric($_GET['depot_id']))
    {
    	make_json_error('invalid param');
    	exit();
    } 	
    $result       = array('error'=>0, 'message'=>'', 'content'=>''); 
    $depot_id     = intval($_GET['depot_id']);
    
    $opt = array(); 
    
    if($depot_id>0){
	 	$arr          = get_seats($depot_id);	 	   
	    foreach ($arr AS $val)
	    {
	        $opt[] = array('value'      => $val['seat_id'],
	                        'text'      => $val['seat_name'],
	                        'data'      => '');
	    }
    }
        
    clear_cache_files();
    
    make_json_result($opt);	
}

elseif($_REQUEST['act']=='seat_range_set'){
    if(empty($_GET['goods_id']) || !is_numeric($_GET['depot_id']))
    {
    	make_json_error('invalid param');
    	exit();
    }
     	
    $result       = array('error'=>0, 'message'=>'', 'content'=>''); 
    $depot_id     = intval($_GET['depot_id']); 
    $goods_id     = intval($_GET['goods_id']);

    $exc->edit('depot_id='.$depot_id, $goods_id);
    
    $opt = array(); 
    
    if($depot_id>0){
	 	$arr          = get_seats($depot_id);	 	   
	    foreach ($arr AS $val)
	    {
	        $opt[] = array('value'      => $val['seat_id'],
	                        'text'      => $val['seat_name'],
	                        'data'      => '');
	    }
    }
    $exc->edit('seat_id=0', $goods_id);  
       
    clear_cache_files();
    
    make_json_result($opt);     
}

elseif($_REQUEST['act']=='change_seat_set'){
    if(empty($_GET['goods_id']) || !is_numeric($_GET['seat_id']))
    {
    	make_json_error('invalid param');
    	exit();
    }	
    
    $result       = array('error'=>0, 'message'=>'', 'content'=>''); 
    $seat_id      = intval($_GET['seat_id']); 
    $goods_id     = intval($_GET['goods_id']);

    $exc->edit('seat_id='.$seat_id, $goods_id);  
    
    clear_cache_files();
    
    make_json_result($opt);       
}

elseif ($_REQUEST['act'] == 'batch')
{	
    /* 取得要操作的记录编号 */
    if (empty($_POST['checkboxes']))
    {
        sys_msg('没有选择记录');
    }
    else
    {
        /* 检查权限 */
//        admin_priv();

        $ids      = $_POST['checkboxes'];
        $depot_id = intval($_POST['depot_id']);
        $seat_id  = intval($_POST['seat_id']);
        
        if (is_array($ids))
        {
            /* 删除记录 */
            $sql = "UPDATE " . $os->table('goods') . " SET depot_id=$depot_id,seat_id=$seat_id WHERE goods_id " . db_create_in($ids);
            $db->query($sql);

            /* 记日志 */
            admin_log('', 'batch', 'goods_depot_seat');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' =>  '返回商品仓库与库位列表', 'href' => 'goods_dopot_seat.php?act=list&' . list_link_postfix());
            sys_msg('批量设置商品仓库与库位成功', 0, $links);
        }
    }	
}
?>